<?php
/**
 * Created by PhpStorm.
 * User: Ysll <754060604@qq.com>
 * Date: 2017/10/7
 * Time: 15:56
 */

namespace Admin\Controller;


use Admin\Model\Product;
use Admin\Model\ProductCategories;
use Common\Common\FileSystem;
use Common\Common\Pagination;
use Common\Common\ResponseResult;
use Common\Common\Validator;
use Think\Think;

class ProductController extends Controller
{
    /**
     * 分类列表
     */
    public function productCategoryList()
    {
        $this->checkPermission('product-category-edit');
        $this->assign('categories', ProductCategories::all());
        $this->display('admin/product/category_list');
    }

    /**
     * 插入分类
     */
    public function insertCategory()
    {
        if (!IS_POST) {
            $this->error('请求错误');
        }
        $validator = Validator::make($_POST, [
            'title' => ['required', 'unique:product_categories,title']
        ]);
        if ($validator->filed()) {
            $this->error('数据错误', '', false, $validator);
        }

        if (ProductCategories::creates(array_only($_POST, ['title']))) {
            $this->success('创建成功');
        } else {
            $this->error('创建失败');
        }
    }

    /**
     * 更新分类数据
     */
    public function updateCategory()
    {
        if (!IS_POST) {
            ResponseResult::error(1000, '请求错误');
        }
        $v = Validator::make($_POST, [
            'id' => ['required', 'exists:product_categories,id'],
            'title' => ['required', 'unique:product_categories,title']
        ]);

        if ($v->filed()) {
            ResponseResult::error(-1000, $v->error());
        }

        if (ProductCategories::update($_POST['id'], array_only($_POST, ['title']))) {
            ResponseResult::success('更新成功');
        }
        ResponseResult::error(1000, '更新失败');
    }

    /**
     * 删除分类数据
     */
    public function deleteCategory()
    {
        if (!IS_POST) {
            ResponseResult::error(1000, '请求错误');
        }
        $v = Validator::make($_POST, [
            'id' => ['required', 'exists:product_categories,id']
        ]);

        if ($v->filed()) {
            ResponseResult::error(-1000, $v->error());
        }

        if (ProductCategories::destroy($_POST['id'])) {
            ResponseResult::success('删除成功');
        }
        ResponseResult::error(1000, '删除失败');
    }

    /**
     * 产品列表
     */
    public function productList()
    {
        $this->checkPermission('product-edit');
        $validator = Validator::make($_REQUEST, [
            'search' => ['min:1'],
            'page' => ['integer']
        ]);
        $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
        $search = $_REQUEST['search'];
        if ($validator->filed()) {
            if (isset($validator->error()['search'])) {
                $search = null;
            }
            if (isset($validator->error()['page'])) {
                $page = 1;
            }
        }

        $where = null;
        if ($search !== null) {
            $where = "title like '%$search%'";
        }
        $products = Pagination::make($page, 30, M('products'), $where);
        foreach ($products->data as $key => $item) {
            $products->data[$key]['category'] =
                ProductCategories::findById($products->data[$key]['product_categories_id'])['title'];
        }
        $this->assign('products', $products);
        $this->assign('search', $search);
        $this->assign('data', $products->data);
        $this->display('admin/product/list');
    }

    /**
     * 添加产品
     */
    public function add()
    {
        $this->checkPermission('product-edit');
        $categories = ProductCategories::all();
        $this->assign('categories', $categories);
        $this->display('admin/product/add');
    }

    /**
     * 编辑产品
     */
    public function edit()
    {
        $this->checkPermission('product-edit');
        $validator = Validator::make($_REQUEST, [
            'id' => 'required|exists:products,id'
        ]);
        if ($validator->filed()) {
            $this->error('参数错误', '', false, $validator);
        }
        $categories = ProductCategories::all();
        $product = Product::findById($_REQUEST['id']);
        $this->assign('categories', $categories);
        $this->assign('product', $product);
        $this->display('admin/product/edit');
    }

    /**
     * 插入产品
     */
    public function insertProduct()
    {
        $this->checkPermission('product-edit');
        if (!IS_POST) {
            $this->error('请求错误');
        }
        $data = array_merge($_FILES, $_POST);
        $validator = Validator::make($data, [
            'product_categories_id' => ['required', 'exists:product_categories,id'],
            'title' => ['required', 'max:64'],
            'icon' => ['required', 'file:image'],
            'description' => ['required', 'max:128'],
            'banner' => ['required', 'file:image'],
            'content' => ['required']
        ]);
        if ($validator->filed()) {
            $this->error('参数错误', '', false, $validator);
        }
        $data = array_only($data, ['product_categories_id', 'title', 'description', 'content']);
        $data['icon'] = FileSystem::uploadFile('icon');
        $data['banner'] = FileSystem::uploadFile('banner');
        if (Product::insert($data)) {
            $this->success('创建成功');
        } else {
            $this->error('保存失败');
        }
    }

    /**
     * 删除产品
     */
    public function deleteProduct()
    {
        $this->checkPermission('product-edit');
        if (!IS_POST) {
            ResponseResult::error(1000, '请求错误');
        }

        $validator = Validator::make($_POST, [
            'id' => ['required', 'exists:products,id']
        ]);
        if ($validator->filed()) {
            ResponseResult::error(2000, $validator->error());
        }
        if (Product::destroy($_POST['id'])) {
            ResponseResult::success('删除成功');
        }
        ResponseResult::error(3000, '删除失败');
    }

    /**
     * 更新产品
     */
    public function updateProduct()
    {
        $this->checkPermission('product-edit');
        if (!IS_POST) {
            $this->error('请求错误');
        }
        $request = array_merge($_POST, $_FILES);
        $validator = Validator::make($request, [
            'id' => 'required|exists:products,id',
            'product_categories_id' => 'required|exists:product_categories,id',
            'title' => 'required|max:64',
            'description' => 'required',
            'icon' => 'file:image',
            'banner' => 'file:image',
            'content' => 'required'
        ]);

        if ($validator->filed()) {
            $this->error('数据错误', '', false, $validator);
        }
        $data = array_only($request, ['product_categories_id', 'title', 'content', 'description']);
        if (isset($request['icon'])) {
            $data['icon'] = FileSystem::uploadFile('icon');
        }
        if (isset($request['banner'])) {
            $data['banner'] = FileSystem::uploadFile('banner');
        }
        if (Product::update($request['id'], $data)) {
            $this->success('保存成功');
        } else {
            $this->error('保存失败');
        }
    }
}